﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Service;
using NHibernate.Criterion;
using ZDSoft.LMS.Manager;

namespace ZDSoft.LMS.Component
{
    public class SystemFunctionComponent : BaseComponent<SystemFunction,SystemFunctionManager>,ISystemFunctionService
    {
        public IList<SystemFunctionDto> GetAllByRoleID(int roleID)
        {
            Role role = new RoleManager().Get(roleID);
            IList<SystemFunctionDto> dtos = new List<SystemFunctionDto>();
            IList<SystemFunction> list = new SystemFunctionManager().GetAll();
            SystemFunctionDto dto = null;
            foreach (SystemFunction f in list)
            {
                dto = Transfor(f, role);
                dtos.Add(dto);
            }
            return dtos;
        }


        public IList<SystemFunctionDto> GetByParentIDAndRoleID(int parentID, int roleID)
        {
            Role role = new RoleManager().Get(roleID);
            IList<SystemFunctionDto> dtos = new List<SystemFunctionDto>();
            IList<ICriterion> cris = new List<ICriterion>();
            cris.Add(Expression.Eq("Parent.ID",parentID));
            IList<SystemFunction> fs = new List<SystemFunction>();
            IList<SystemFunction> functions = new SystemFunctionManager().Find(cris);//无法实现递归查询,所以手工加入到集合
            foreach (SystemFunction f in functions)
            {
                fs.Add(f);
                if (f.Children != null)
                {
                    foreach (SystemFunction cf in f.Children)
                    {
                        fs.Add(cf);
                    }
                }
            }

            SystemFunctionDto dto = null;
            foreach (SystemFunction f in fs)
            {
                dto = Transfor(f, role);
                dtos.Add(dto);
            }
            return dtos;
        }

        private SystemFunctionDto Transfor(SystemFunction f,Role role)
        {
            SystemFunctionDto dto = new SystemFunctionDto();
            dto.ID = f.ID;
            dto.Name = f.SystemFunctionName;
            dto.ParentID = (f.Parent == null) ? 0 : f.Parent.ID;
            if (role.SystemFunctions.Where(o => o.ID == f.ID).Count() > 0)
            {
                dto.IsChecked = true;
            }
            return dto;
        }


        public IList<SystemFunction> GetTopFunction()
        {
            return new SystemFunctionManager().GetAll().Where(o => o.Parent == null).ToList();
        }

        /// <summary>
        /// 通过功能ID查询子功能
        /// </summary>
        /// <param name="functionID"></param>
        /// <returns></returns>
        public IList<SystemFunction> GetByFunctionID(int? functionID)
        {
            IList<ICriterion> cris = new List<ICriterion>();
            if (functionID.HasValue)
            {
                cris.Add(Expression.Eq("Parent.ID", functionID.Value));
                return manager.Find(cris);
            }
            
            return manager.GetAll();

        }
    }
}
